package com.wsj.a.collapsingdemo.db;

import io.realm.RealmConfiguration;

/**
 * Created by linxiujie on 2018/8/15.
 * TODO:操作数据库
 * http://www.jianshu.com/p/01a9c7f3c084
 * http://www.jianshu.com/p/95f62e739d1b
 * http://www.open-open.com/lib/view/open1478507176480.html
 */

public class RealmHelper {
    //数据库名
    private final static String DB_NAME = "Data.realm";
    //数据库版本号
    private final static int DB_VERSION = 1;
    //单例
    private static RealmHelper instance;
    //数据库的配置信息
    private static RealmConfiguration configuration;

    private RealmHelper() {
        //数据库加密
        byte[] key = new byte[64];
        for (byte i = 0; i < 64; i++) {
            key[i] = i;
        }
        //数据库配置信息
        configuration = new RealmConfiguration.Builder()
                .name(RealmHelper.DB_NAME)//数据名称
                .schemaVersion(RealmHelper.DB_VERSION)//版本号
                .encryptionKey(key)//加密保存,暂时有问题
                .deleteRealmIfMigrationNeeded()//版本升级
                .build();
    }

    public static RealmConfiguration getConfiguration() {
        return configuration;
    }

    public static RealmHelper getInstance() {
        if (instance == null) {
            synchronized (RealmHelper.class) {
                if (instance == null) {
                    instance = new RealmHelper();
                }
            }
        }
        return instance;
    }




//    /**
//     * 保存用户信息
//     *
//     * @param user
//     */
//    public void save(User user) {
//        Realm realm = Realm.getInstance(configuration);
//        realm.beginTransaction();
//
//        User member = realm.createObject(User.class);
//
//        member.id = user.id;
//        member.username = user.username;
//        member.nickname = user.nickname;
//        member.mobile = user.mobile;
//        member.avatar = user.avatar;
//        member.score = user.score;
//        member.token = user.token;
//
//
//        realm.commitTransaction();
//        realm.close();
//    }
//
//    /**
//     * 删除保存的用户信息
//     */
//    public void delete() {
//        Realm realm = Realm.getInstance(configuration);
//        realm.beginTransaction();
//        realm.where(User.class).findAll().deleteAllFromRealm();
//        realm.commitTransaction();
//        realm.close();
//    }
//
//
//    /**
//     * 获取保存的用户信息
//     *
//     * @return
//     */
//    public User get() {
//        Realm realm = Realm.getInstance(configuration);
//        User user = realm.where(User.class).findFirst();
//
//        if (user == null) {
//            return null;
//        }
//
//        return realm.copyFromRealm(user);
//    }
//
//    /**
//     * 修改保存的用户信息
//     */
//    public void edit(User user) {
//        Realm realm = Realm.getInstance(configuration);
//        User member =realm.where(User.class).equalTo("id", user.id).findFirst();
//        realm.beginTransaction();
//
//        if(member!=null){
//            member.id = user.id;
//            member.username = user.username;
//            member.nickname = user.nickname;
//            member.mobile = user.mobile;
//            member.avatar = user.avatar;
//            member.score = user.score;
//            member.token = user.token;
//        }
//
//        realm.commitTransaction();
//        realm.close();
//    }
}
